/**
 * Project: push-center-web-home
 * 
 * File Created at 2014年10月10日
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package com.greenline.push.home.controllers.category;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.greenline.guser.annotation.GuserResource;
import com.greenline.push.common.json.BaseJson;
import com.greenline.push.common.json.GridJsonData;
import com.greenline.push.core.pushcenter.CacheCategoryManager;
import com.greenline.push.core.pushcenter.CategoryManager;
import com.greenline.push.dal.pushcenter.dataobject.CategoryDO;
import com.greenline.push.dal.query.CategoryQuery;

/**
 * @Type CategoryController
 * @Desc 分类
 * @author zhaoli
 * @date 2014年10月10日
 * @Version V1.0
 */
@Controller
@RequestMapping(value = "/category")
public class CategoryController {
    @Resource
    private CategoryManager categoryManager;
    @Resource
    private CacheCategoryManager cacheCategoryManager;

    private final Log logger = LogFactory.getLog(CategoryController.class);

    @GuserResource("分类配置列表")
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public String queryRecord(ModelMap model) {
        return "category/category_list";
    }

    @GuserResource("分类配置列表")
    @RequestMapping("/query.json")
    @ResponseBody
    public GridJsonData<CategoryDO> listRecord(CategoryQuery query,
            @RequestParam(value = "page", defaultValue = "1", required = false) String pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) String pageSize) {
        query.setPageIndex(Integer.parseInt(pageIndex));
        query.setPageSize(Integer.parseInt(pageSize));
        GridJsonData<CategoryDO> ret = new GridJsonData<CategoryDO>();
        ret.setQuery(categoryManager.listByQuery(query));
        return ret;
    }

    @GuserResource("分类配置添加")
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addInit(ModelMap model) {
        return "category/category_add";
    }

    @GuserResource("分类配置添加")
    @RequestMapping(value = "/add.json", method = RequestMethod.POST)
    public @ResponseBody
    BaseJson add(ModelMap model, @ModelAttribute("category") CategoryDO category) {
        BaseJson json = new BaseJson();
        try {
            if (categoryManager.insert(category) > 0) {
                json.setMessage("提交成功！");
            } else {
                json.setHasError(true);
                json.setMessage("提交出错");
            }
        } catch (Exception e) {
            json.setHasError(true);
            json.setMessage("提交出错");
            logger.error(json.getMessage(), e);
        }
        return json;
    }

    @GuserResource("分类配置修改")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String updateInit(ModelMap model, @PathVariable Long id) {
        CategoryDO object = categoryManager.getCategoryById(id);
        model.put("category", object);
        return "category/category_update";
    }

    @GuserResource("分类配置修改")
    @RequestMapping(value = "/update.json", method = RequestMethod.POST)
    public @ResponseBody
    BaseJson update(ModelMap model, @ModelAttribute("category") CategoryDO category) {
        BaseJson json = new BaseJson();
        try {
            if (categoryManager.update(category)) {
                json.setMessage("提交成功！");
            } else {
                json.setHasError(true);
                json.setMessage("提交出错");
            }
        } catch (Exception e) {
            json.setHasError(true);
            json.setMessage("提交出错");
            logger.error(json.getMessage(), e);
        }
        return json;
    }

    @GuserResource("分类配置删除")
    @RequestMapping(value = "/delete.json", method = RequestMethod.POST)
    public @ResponseBody
    BaseJson delete(ModelMap model, @ModelAttribute("id") Long id) {
        BaseJson json = new BaseJson();
        try {
            if (categoryManager.delete(id)) {
                json.setMessage("删除成功！");
            } else {
                json.setHasError(true);
                json.setMessage("删除出错");
            }
        } catch (Exception e) {
            json.setHasError(true);
            json.setMessage("删除出错");
            logger.error(json.getMessage(), e);
        }
        return json;
    }

    @GuserResource("分类列表")
    @RequestMapping(value = "/list-all.json", method = RequestMethod.POST)
    public @ResponseBody
    List<CategoryDO> listAll(ModelMap model) {
        return cacheCategoryManager.listCacheCategory();
    }
}
